<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dettaglio alunno</title>
<link href="../../css/main.css" rel="stylesheet" />
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javaScript">
function go(par, proj){
	//alert(document.forms[0].teachers.value);
	document.forms[0].teachers.value = document.forms[0].teachers.value.replace(/#$/,"");
	//alert(document.forms[0].teachers.value);
	var numero_documenti = <?php print $count_docs ?>;
	
    if(par == 2){
    	if(numero_documenti > 0){
			alert("Il progetto non puo' essere cancellato perche' sono presenti dei documenti associati allo stesso");
			return false;
		}
        if(!confirm("Sei sicuro di voler cancellare questo progetto?"))
            return false;
    }

    $('_i').value = proj;
    $('action').value = par;
    var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_projects/project_manager.php";
    //alert(url);
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: $('proj_form').serialize(true),
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		//alert(response);
			      		var dati = response.split("|");
			      		if(dati[0] == "ko"){
							alert("Errore SQL. \nQuery: "+dati[1]+"\nErrore: "+dati[2]);
							return;
			      		}
			      		link = "progetti.php?msg="+par;
			      		if(par != 1){
							link += "&second=1&offset=<?php print ($offset ? $offset : 0)  ?>";
			      		}

						parent.document.location.href = link;
			      		//parent.win.close();
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...");}
			  });
}

function get_teacher(nuovo){
	/*
	funzione che gestisce i referenti, chiamando con AJAX il db se nuovo = 1
	e cancellando invece il contenuto della textarea se nuovo = 0
	*/
	if(nuovo == 0){
		document.forms[0].referenti.value = "";
		document.forms[0].teachers.value = "";
	}
	else if(nuovo == 1){
		//var mywin = window.open_centered("elenco_docenti.php", "referer", 650, 600, "");
		parent.win2.show(false);
		parent.win2.toFront();
	}
}

</script>
</head>
<body <?php if(isset($msg)) print("onLoad=\"alert('".$msg."'); window.close(); window.opener.document.location.href='progetti.php'\"") ?>>
    <p class="popup_header">Gestione progetto</p>
    <form action="dettaglio_progetto.php?upd=1" method="post" id="proj_form">
    <div style="margin-right: auto; margin-left: auto; margin-top: 10px; width: 500px">
    	<fieldset style="width: 500px; border: 1px solid; padding-top: 10px; ">
	    <legend style="font-weight: bold;">Progetto</legend>
	    <table style="width: 480px; margin-right: auto; margin-left: auto">
	        <tr class="popup_row header_row">
	            <td class="popup_title" style="width: 150px; padding-left: 10px; ">Nome</td>
	            <td style="width: 330px; " colspan="2">
	            	<input class="form_input" type="text" name="nome" style="width: 330px" value="<?php if(isset($project)) print $project['nome'] ?>"  />
	            </td>
	        </tr>
	        <tr class="popup_row">
	        	<td class="popup_title" style="width: 150px; padding-left: 10px">Referenti</td>
	            <td style="width: 205px; vertical-align: middle">
	                <textarea class="form_input" name="referenti" style="width: 205px; height: 50px;"><?php if(isset($teachers_string)){ print join("\n", $teachers_string);} ?></textarea>
	            </td>
	            <td style="width: 125px; text-align: right">
	             	<a href="#" onclick="get_teacher(1)">Add</a>&nbsp;&nbsp;|&nbsp;&nbsp;
	                <a href="#" onclick="get_teacher(0)">Delete</a>
	            </td>
	        </tr>
	        <tr class="popup_row">
	        	<td class="popup_title" style="width: 150px; padding-left: 10px">Descrizione</td>
	            <td colspan="2">
	                <textarea class="form_input" name="descrizione" style="width: 330px; height: 75px; margin-top: auto; margin-bottom: auto"><?php if(isset($project)) print $project['descrizione'] ?></textarea>
	            </td>
	        </tr>
	        <tr class="popup_row">
	        	<td class="popup_title" style="width: 150px; padding-left: 10px">Anno inizio</td>
	            <td style="width: 205px; vertical-align: middle">
	                <select class="form_input" style="width: 200px; font-size: 11px; color: #777" name="anno">
	            <?php
				while($year = $res_anni->fetch_assoc()){
	            ?>
	            		<option <?php if($year['id_anno'] == $project['anno_inizio']) print("selected='selected'") ?> value="<?php print $year['id_anno'] ?>"><?php print $year['descrizione'] ?></option>
	            <?php } ?>
	                </select>
	            </td>
	            <td class="popup_title" style="width: 125px; padding-left: 30px">Attivo
	            	<input class="form_input" style="padding-left: 5px" type="radio" name="attivo" id="attivo" value="1" <?php if($project['attivo'] == 1 || $_i == 0) print("checked='checked'") ?> /> <label style="font-weight: normal" for="attivo">SI</label> <input type="radio" name="attivo" value="0" <?php if($project['attivo'] == 0 && $_i != 0) print("checked='checked'") ?> /> <label style="font-weight: normal" for="attivo">NO</label>
	            </td>
	        </tr>
	        <tr class="popup_row">
	            <td colspan="4"></td>
	        </tr>
	    </table>
	    </fieldset>
	    <div style="width: 500px; text-align: right; margin-top: 20px">
	    	<a href="#" onclick="go(<?php if(isset($_GET['id']) && $_GET['id'] != 0) print("3, ".$_REQUEST['id']); else print("1, 0"); ?>)" class="nav_link_first">Registra</a>|
	    	<?php if(isset($_GET['id']) && $_GET['id'] != 0){
	        ?>
	        <a href="#" onclick="go(2, <?php print $_REQUEST['id'] ?>)" class="nav_link">Cancella</a>|
	        <?php
	        }
	        ?>
	        <a href="#" onclick="parent.win.close()" class="nav_link_last">Chiudi</a>
	        <input type="hidden" name="action" id="action" />
		    <input type="hidden" name="_i" id="_i" />
		    <input type="hidden" name="teachers" id="teachers" value="<?php print $project['referenti'] ?>" />
	    </div>
    </div>
    </form>
</body>
</html>